package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwSceBasicinfo;
import com.xwdeshj.entity.XwBusinessInfo;
import com.xwdeshj.entity.XwFarmBasicinfo;
import com.xwdeshj.entity.XwScenicBasicinfo;
import com.xwdeshj.entity.XwUserinfo;
import com.xwdeshj.repository.XwBusinessInfoRepository;
import com.xwdeshj.repository.XwScenicBasicinfoRepository;
import com.xwdeshj.repository.XwUserinfoRepository;
import com.xwdeshj.service.XwScenicBasicinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by BKWJA on 2018/12/24.
 * 景区店铺基本信息实现类
 */
@Slf4j
@Service("xwScenicBasicinfoService")
public class XwScenicBasicinfoServiceImpl implements XwScenicBasicinfoService {

    @Autowired
    private XwScenicBasicinfoRepository xwScenicBasicinfoRepository;
    @Autowired
    private XwBusinessInfoRepository xwBusinessInfoRepository;
    @Autowired
    private XwUserinfoRepository xwUserinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_XwSceBasicinfo dal_xwSceBasicinfo = new Dal_XwSceBasicinfo();

    @Override
    public JSONObject gettourinfolist(Integer pagesize, Integer pageindex, String starttime, String endtime,
                                      String searchval, String searchkey, String sortfild, String sortway,
                                      String type, String grade, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){

            result = dal_xwSceBasicinfo.gettourinfolist(pagesize,pageindex,starttime,endtime,searchval,searchkey,
                    sortfild,sortway,type,grade,jdbcTemplate);

        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("gettourinfolist:"+result.toString());
        return result;
    }

    /**
     *  景区——商家详情——获取农家乐基本信息（一条）
     */
    @Override
    public JSONObject getfarminfoOne(String business_encryptId, String sceinc_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        if(!"".equals(business_encryptId) && !"".equals(sceinc_encryptId)){
            result = dal_xwSceBasicinfo.getfarminfoOne(business_encryptId,sceinc_encryptId,jdbcTemplate);
        }else {
            result.put("status",40002);
            result.put("msg","business_encryptId,farm_encryptId不能为空！");
        }
        System.out.println("getfarminfo:"+result.toString());
        return result;
    }

    /**
     * 景区——修改商家的游玩类型备注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject modifysceincRemarks(String sceinc_encryptId, String themeRemarks,String theme_encryptId,
                                              String playRemarks,String play_encryptId,String crowdRemarks,
                                              String crowd_encryptId,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!"".equals(sceinc_encryptId)){

                XwScenicBasicinfo xwScenicBasicinfo = new XwScenicBasicinfo();
                xwScenicBasicinfo.setScenicEncryptid(sceinc_encryptId);
                boolean temp = xwScenicBasicinfoRepository.exists(Example.of(xwScenicBasicinfo));
                if(temp){
                    try {
                        XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(sceinc_encryptId);
                        scenicBasicinfo.setScenicCrowdname(crowdRemarks);
                        scenicBasicinfo.setSceniCrowdEncryptid(crowd_encryptId);
                        scenicBasicinfo.setScenicThemeDesc(themeRemarks);
                        scenicBasicinfo.setScenicThemeEncryptid(theme_encryptId);
                        scenicBasicinfo.setScenicPalytypeDesc(playRemarks);
                        scenicBasicinfo.setScenicPlaytypeEncryptid(play_encryptId);
                        scenicBasicinfo.setScenicUpdatetime(new Date());
                        xwScenicBasicinfoRepository.save(scenicBasicinfo);
                        result.put("status",0);
                        result.put("msg","ok");
                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }

                }else {
                    result.put("status",40002);
                    result.put("msg","该店铺不存在！");
                }

            }else {
                result.put("status",40002);
                result.put("msg","sceinc_encryptId不能为空！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("modifysceincRemarks:"+result.toString());
        return result;
    }


    /**
     * 景区——更改店铺基本信息
     */
    @Override
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    public JSONObject updateSceincinfo(String business_encryptId, String scenic_encryptId, String updatefiled,
                                       String updateval, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!"".equals(scenic_encryptId)){

                //验证农家信息是否存在
                XwScenicBasicinfo xwScenicBasicinfo = new XwScenicBasicinfo();
                xwScenicBasicinfo.setScenicEncryptid(scenic_encryptId);
                boolean temp = xwScenicBasicinfoRepository.exists(Example.of(xwScenicBasicinfo));
                //验证商家信息是否存在
                XwBusinessInfo xwBusinessInfo = new XwBusinessInfo();
                xwBusinessInfo.setBusinessEncryptid(business_encryptId);
                xwBusinessInfo.setSecEncryptid(scenic_encryptId);
                boolean businsstemp = xwBusinessInfoRepository.exists(Example.of(xwBusinessInfo));
                if(temp && businsstemp){
                    try {
                        if(updatefiled.equals("tel")){    //修改电话

                            //第一步 ：应查询该电话号码是否 已经存在（1、用户信息表）
                            List<XwUserinfo> user = xwUserinfoRepository.findByUserTel(updateval.trim());
                            if(user.size() == 0){
                                //第二步：不存在则修改成功，反之，提示：您要修改的电话已经有人注册，请重新添加电话
                                XwBusinessInfo businessInfo = xwBusinessInfoRepository.findByBusinessEncryptidAndSecEncryptid(business_encryptId,scenic_encryptId);
                                businessInfo.setBusinessTel(updateval);
                                businessInfo.setBusinessUpdatetime(new Date());
                                xwBusinessInfoRepository.save(businessInfo);

                                //第三步：不存在的情况下，在修改商家的电话号码同时修改用户信息表改商家的电话号码
                                XwUserinfo userinfo = new XwUserinfo();
                                userinfo.setUserEncryptid(business_encryptId);
                                boolean userTeltemp = xwUserinfoRepository.exists(Example.of(userinfo));
                                if(userTeltemp){
                                    XwUserinfo xwUserinfo = xwUserinfoRepository.findByUserEncryptid(business_encryptId);
                                    xwUserinfo.setUserTel(updateval);
                                    xwUserinfoRepository.save(xwUserinfo);
                                }

                                result.put("status",0);
                                result.put("msg","ok");

                            }else {
                                //已被人注册情况下
                                result.put("status",40002);
                                result.put("msg","联系电话已被他人注册！");
                            }

                        }else if(updatefiled.equals("city")){ //修改城市
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicCity(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("district")){ //修改区县
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicDistrict(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("develop_time")){ //开放时间
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicDevelopTime(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("ticket")){ //门票价格
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicTicket(Double.parseDouble(updateval));
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("address")){ //住所
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicAddress(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("season")){ //最佳季节
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicSeason(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("grade")){ //景区等级
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicGrade(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("bus_route")){ //路线情况（公交路线）
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicBusRoute(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("self_route")){ //路线情况（自驾路线）
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicSelfRoute(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("scenic_Intr_info")){ //商家介绍
                            //店铺信息
                            XwScenicBasicinfo scenicBasicinfo = xwScenicBasicinfoRepository.findByScenicEncryptid(scenic_encryptId);
                            scenicBasicinfo.setScenicIntrInfo(updateval);
                            xwScenicBasicinfo.setScenicUpdatetime(new Date());
                            xwScenicBasicinfoRepository.save(scenicBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }

                }else {
                    result.put("status",40002);
                    result.put("msg","该店铺不存在！");
                }

            }else {
                result.put("status",40002);
                result.put("msg","farm_encryptId不能为空！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("modifyfarmplayRemarks:"+result.toString());
        return result;
    }

}
